#include "settingdlg.h"
#include "ui_settingdlg.h"
#include <QSettings>
#include <QTranslator>

#include "start.h"
#include <QPainter>

#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QFileDialog>
#include <QFileInfo>

#include <QHeaderView>
#include <QSqlQuery>
#include <QFont>
#include <QScrollBar>
#include <QDate>
#include <QDebug>
#include <QMessageBox>
#include <QScreen>
#include <QDir>

QString SettingDlg::ConfigFile = QDir::currentPath() + "/config.ini";
//QString SettingDlg::ConfigFile = "/home/esm/software/linuxQt/build-MD200-Desktop_Qt_6_6_1_GCC_64bit-Debug/config.ini";
SettingDlg::SettingDlg(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::SettingDlg)
{
    QLabel *bk0 = new QLabel(this);
    bk0->setGeometry(0, 0, 1024, 44);
    bk0->setStyleSheet( "background:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE);" );

    ui->setupUi(this);
    setWindowTitle(tr("设置"));

#ifdef Q_OS_LINUX
    this->setWindowFlags(Qt::FramelessWindowHint);//无边框设置
    this->showFullScreen();
#endif

//    QRect screenRect = QGuiApplication::primaryScreen()->geometry();
//    move((screenRect.width() - this->width())/2, (screenRect.height() - this->height())/2);
    ui->tabWidget->setStyleSheet( "QTabWidget{background-color:black;} QTabWidget::pane{border:1px;} "
                                  "QTabWidget::tab-bar{alignment:left;} "
                                  "QTabBar::tab{background:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE); color:white;min-width:204px; min-height:44px;} "
                                  "QTabBar::tab:hover{background:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #9903AB,stop:1 #098DCE);color:black;} "
                                  "QTabBar::tab:selected{border-color: black;background:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ff03AB,stop:1 #ff8DCE); color:black;}" );

    //init DB
    if (!db.isValid()) {
        //需在addDatabase函数的第二个参数，加上一个我们自定义的连接名即可，因为如不加的话，它是用的默认连接名qt_sql_default_connection
        db = QSqlDatabase::addDatabase("QODBC", "jzdb2");
        db.setDatabaseName("jzdb2");//

        bool ok = db.open();
        if (ok){
            qDebug()<<"jzdb2::success";
        } else {
            qDebug()<<"jzdb2::failed";
        }
    }

    initTab0();
    initTab1();
    initTab2();
    initTab3();
    initTab4();

    ui->tabWidget->setCurrentIndex(0);
}

SettingDlg::~SettingDlg()
{
    delete ui;
}

void SettingDlg::initTab0()
{
    QSettings set(SettingDlg::ConfigFile, QSettings::IniFormat);
    QString strLanguage = set.value("language", "0").toString();
    lastLanguage = strLanguage.toInt();
    ui->comboBox->setCurrentIndex(lastLanguage);

    bool bHasRFID = set.value("bHasRFID", "false").toBool();
    if (bHasRFID) {
         ui->comboBox_2->setCurrentIndex(0);
    } else {
        ui->comboBox_2->setCurrentIndex(1);
    }

    QString mLinename = set.value("linename", "").toString();
    ui->lineEdit->setText(mLinename);
}

void SettingDlg::initTab1()
{
    ui->timeEdit_from->setDisplayFormat("HH:mm");
    ui->timeEdit_to->setDisplayFormat("HH:mm");
    initListview1();
    getData1();
}

void SettingDlg::initTab2()
{
    ui->dateEdit_jzlog->setDate(QDate::currentDate());
    ui->dateEdit_jzlog->setDisplayFormat("yyyy-MM-dd");
    initListview2();
    getData2();
}

void SettingDlg::initTab3()
{
    ui->dateEdit_dzlog->setDate(QDate::currentDate());
    ui->dateEdit_dzlog->setDisplayFormat("yyyy-MM-dd");

    initListview3();
    getData3();
}

void SettingDlg::initTab4()
{
    ui->dateEdit_polog->setDate(QDate::currentDate());
    ui->dateEdit_polog->setDisplayFormat("yyyy-MM-dd");

    initListview4();
    getData4();
}

void SettingDlg::initListview1()
{
    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);
    ui->tableWidget_jytime->horizontalHeader()->setFont(font);

    ui->tableWidget_jytime->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    ui->tableWidget_jytime->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
    ui->tableWidget_jytime->setSelectionMode(QAbstractItemView::SingleSelection); //不选
    ui->tableWidget_jytime->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tableWidget_jytime->setAlternatingRowColors(true);
    ui->tableWidget_jytime->verticalHeader()->setVisible(false);
    ui->tableWidget_jytime->horizontalHeader()->setHighlightSections(false);

    //设置表头
    ui->tableWidget_jytime->horizontalHeader()->setStyleSheet("QHeaderView::section{height:50px; background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE);color: #ABD6FF;padding-left: 4px;border: 1px solid #383838;}");
    ui->tableWidget_jytime->verticalHeader()->setDefaultSectionSize(58);
    ui->tableWidget_jytime->verticalScrollBar()->setStyleSheet("QScrollBar{background-color:rgb(1, 137, 255); width:10px;}"
                                                               "QScrollBar::handle{background-color:rgb(1, 137, 255); border:2px solid transparent; border-radius:5px;}"
                                                               "QScrollBar::handle:hover{background-color:rgb(1, 137, 255);}"
                                                               "QScrollBar::sub-line{background:transparent;}"
                                                               "QScrollBar::add-line{background:transparent;}");
    //设置列数和列宽
    int width = this->width();
    ui->tableWidget_jytime->setColumnCount(4);
    ui->tableWidget_jytime->setColumnWidth(0, 95);
    ui->tableWidget_jytime->setColumnWidth(1, 277);
    ui->tableWidget_jytime->setColumnWidth(2, 273);
    ui->tableWidget_jytime->setColumnWidth(3, 93);
}

void SettingDlg::getData1()
{
    ui->tableWidget_jytime->clearContents();
//    QStringList headText;
//    headText << tr("序号") << tr("开始时间") << tr("结束时间") << tr("已做");
//    ui->tableWidget_jytime->setColumnCount(4);
//    ui->tableWidget_jytime->setHorizontalHeaderLabels(headText);
    idList1.clear();

    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);

    if (db.isOpen()) {
        QString sql = QString("SELECT * from jiaoyanshezhi where sn = '%1' order by starttime asc").arg(Start::getInstance().strSn);
        QSqlQuery query(db);
        query.exec(sql);

        int prinfonum = 0;
        if (query.last()) {
            prinfonum = query.at() + 1;
        }

        query.first();//重新定位指针到结果集首位
        query.previous();//如果执行query.next来获取结果集中数据，要将指针移动到首位的上一位。
        ui->tableWidget_jytime->setRowCount(prinfonum);
        int i = 0;
        while(query.next()) {
            QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(i + 1));
            int id = query.value("id").toInt();
            QString starttime = query.value("starttime").toString();
            QTableWidgetItem *item1 = new QTableWidgetItem(starttime);
            QString endtime = query.value("endtime").toString();
            QTableWidgetItem *item2 = new QTableWidgetItem(endtime);
            QString zt = query.value("zt").toString();

            if (zt == "1") {
                zt = "OK";
            } else {
                zt = "NG";
            }
            QTableWidgetItem *item3 = new QTableWidgetItem(zt);
            idList1 << id;

            item0->setBackground(QColor(180, 214, 242));
            item0->setFont(font);
            item0->setForeground(QColor(0, 0, 0));
            item0->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

            item1->setBackground(QColor(180, 214, 242));
            item1->setFont(font);
            item1->setForeground(QColor(0, 0, 0));
            item1->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

            item2->setBackground(QColor(180, 214, 242));
            item2->setFont(font);
            item2->setForeground(QColor(0, 0, 0));
            item2->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

            item3->setBackground(QColor(180, 214, 242));
            item3->setFont(font);
            item3->setForeground(QColor(0, 0, 0));
            item3->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

            ui->tableWidget_jytime->setItem(i, 0, item0);
            ui->tableWidget_jytime->setItem(i, 1, item1);
            ui->tableWidget_jytime->setItem(i, 2, item2);
            ui->tableWidget_jytime->setItem(i, 3, item3);
            i++;
        }

        //补齐剩余的行
        //        int buyu = (12 - prinfonum % 12);
        //        ui->tableWidget_jytime->setRowCount(prinfonum + buyu);
        //        for (int i = prinfonum; i < prinfonum + buyu; i++) {
        //            for (int j = 0; j < 5; j++) {
        //                QTableWidgetItem *item= new QTableWidgetItem("");
        //                item->setBackground(QColor(180, 214, 242));
        //                item->setFont(font);
        //                item->setForeground(QColor(0, 0, 0));
        //                item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //                item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        //                ui->tableWidget_jytime->setItem(i, j, item);
        //            }
        //        }

    }

}

void SettingDlg::initListview2()
{
    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);
    ui->tableWidget_jzlog->horizontalHeader()->setFont(font);

    ui->tableWidget_jzlog->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    ui->tableWidget_jzlog->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
    ui->tableWidget_jzlog->setSelectionMode(QAbstractItemView::NoSelection); //不选
    ui->tableWidget_jzlog->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tableWidget_jzlog->setAlternatingRowColors(true);
    ui->tableWidget_jzlog->verticalHeader()->setVisible(false);
    ui->tableWidget_jzlog->horizontalHeader()->setHighlightSections(false);

    //设置表头
    ui->tableWidget_jzlog->horizontalHeader()->setStyleSheet("QHeaderView::section{height:50px; background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE);color: #ABD6FF;padding-left: 4px;border: 1px solid #383838;}");
    ui->tableWidget_jzlog->verticalHeader()->setDefaultSectionSize(58);
    ui->tableWidget_jzlog->verticalScrollBar()->setStyleSheet("QScrollBar{background-color:rgb(1, 137, 255); width:10px;}"
                                                              "QScrollBar::handle{background-color:rgb(1, 137, 255); border:2px solid transparent; border-radius:5px;}"
                                                              "QScrollBar::handle:hover{background-color:rgb(1, 137, 255);}"
                                                              "QScrollBar::sub-line{background:transparent;}"
                                                              "QScrollBar::add-line{background:transparent;}");
    //设置列数和列宽
    int width = this->width();
    ui->tableWidget_jzlog->setColumnCount(5);
    ui->tableWidget_jzlog->setColumnWidth(0, 95);
    ui->tableWidget_jzlog->setColumnWidth(1, 277);
    ui->tableWidget_jzlog->setColumnWidth(2, 273);
    ui->tableWidget_jzlog->setColumnWidth(3, 93);
    ui->tableWidget_jzlog->setColumnWidth(4, 100);
}

void SettingDlg::getData2()
{
    ui->tableWidget_jzlog->clearContents();
//    QStringList headText;
//    headText << tr("序号") << tr("时刻") << tr("漏检") << tr("检出") << tr("结果");
//    ui->tableWidget_jzlog->setColumnCount(5);
//    ui->tableWidget_jzlog->setHorizontalHeaderLabels(headText);

    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);

    QString riqi = ui->dateEdit_jzlog->text();

    if (db.isOpen()) {
        QString sql = QString("SELECT * from jiaoyaninfo where riqi='%1' and sn='%2' ORDER BY shijian")
                .arg(riqi, Start::getInstance().strSn);
        QSqlQuery query(db);
        query.exec(sql);

        int prinfonum = 0;
        if (query.last()) {
            prinfonum = query.at() + 1;
        }

        query.first();//重新定位指针到结果集首位
        query.previous();//如果执行query.next来获取结果集中数据，要将指针移动到首位的上一位。
        ui->tableWidget_jzlog->setRowCount(prinfonum + 1);

        int i = 0;
        if (i == 0) {
            QTableWidgetItem *item0 = new QTableWidgetItem("总数");
            QTableWidgetItem *item1 = new QTableWidgetItem("-->");
            QTableWidgetItem *item2 = new QTableWidgetItem("0");
            QTableWidgetItem *item3 = new QTableWidgetItem("0");
            QTableWidgetItem *item4 = new QTableWidgetItem("0");

            item0->setBackground(QColor(180, 214, 242));
            item0->setFont(font);
            item0->setForeground(QColor(0, 0, 0));
            item0->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item0->setFlags(item0->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item1->setBackground(QColor(180, 214, 242));
            item1->setFont(font);
            item1->setForeground(QColor(0, 0, 0));
            item1->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item1->setFlags(item1->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item2->setBackground(QColor(180, 214, 242));
            item2->setFont(font);
            item2->setForeground(QColor(0, 0, 0));
            item2->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item2->setFlags(item2->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item3->setBackground(QColor(180, 214, 242));
            item3->setFont(font);
            item3->setForeground(QColor(0, 0, 0));
            item3->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item3->setFlags(item3->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item4->setBackground(QColor(180, 214, 242));
            item4->setFont(font);
            item4->setForeground(QColor(0, 0, 0));
            item4->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item4->setFlags(item4->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            ui->tableWidget_jzlog->setItem(i, 0, item0);
            ui->tableWidget_jzlog->setItem(i, 1, item1);
            ui->tableWidget_jzlog->setItem(i, 2, item2);
            ui->tableWidget_jzlog->setItem(i, 3, item3);
            ui->tableWidget_jzlog->setItem(i, 4, item4);
            i++;
        }

        int loujian_num = 0;
        int jianchu_num = 0;
        int jieguo_num = 0;
        while(query.next()) {
            QTableWidgetItem *item0 = new QTableWidgetItem(QString::number(i));
            QString shijian = query.value("shijian").toString();
            QTableWidgetItem *item1 = new QTableWidgetItem(shijian);
            int bangloujian = query.value("bangloujian").toInt();
            QTableWidgetItem *item2 = new QTableWidgetItem(QString::number(bangloujian));
            int bangjianchu = query.value("bangjianchu").toInt();
            QTableWidgetItem *item3 = new QTableWidgetItem(QString::number(bangjianchu));

            int jieguo = query.value("jieguo").toInt();
            QString strJ = "";
            QColor fontColor = QColor(0, 0, 0);

            if (jieguo == 0) {
                strJ = "OK";
            } else {
                strJ=  "NG";
                fontColor = QColor(255, 0, 0);
            }
            QTableWidgetItem *item4 = new QTableWidgetItem(strJ);

            loujian_num += bangloujian;
            jianchu_num += bangjianchu;
            jieguo_num += jieguo;

            item0->setBackground(QColor(180, 214, 242));
            item0->setFont(font);
            item0->setForeground(fontColor);
            item0->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item0->setFlags(item0->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item1->setBackground(QColor(180, 214, 242));
            item1->setFont(font);
            item1->setForeground(fontColor);
            item1->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item1->setFlags(item1->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item2->setBackground(QColor(180, 214, 242));
            item2->setFont(font);
            item2->setForeground(fontColor);
            item2->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item2->setFlags(item2->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item3->setBackground(QColor(180, 214, 242));
            item3->setFont(font);
            item3->setForeground(fontColor);
            item3->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item3->setFlags(item3->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            item4->setBackground(QColor(180, 214, 242));
            item4->setFont(font);
            item4->setForeground(fontColor);
            item4->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            item4->setFlags(item4->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

            ui->tableWidget_jzlog->setItem(i, 0, item0);
            ui->tableWidget_jzlog->setItem(i, 1, item1);
            ui->tableWidget_jzlog->setItem(i, 2, item2);
            ui->tableWidget_jzlog->setItem(i, 3, item3);
            ui->tableWidget_jzlog->setItem(i, 4, item4);
            i++;
        }

        int rowSize = ui->tableWidget_jzlog->rowCount();
        if (rowSize > 0) {
            QTableWidgetItem *item02 = ui->tableWidget_jzlog->item(0, 2);
            item02->setText(QString::number(loujian_num));

            QTableWidgetItem *item03 = ui->tableWidget_jzlog->item(0, 3);
            item03->setText(QString::number(jianchu_num));

            QTableWidgetItem *item04 = ui->tableWidget_jzlog->item(0, 4);
            item04->setText(QString::number(jieguo_num));
        }

        //补齐剩余的行
        //        int buyu = (12 - prinfonum % 12);
        //        ui->tableWidget_jzlog->setRowCount(prinfonum + buyu);
        //        for (int i = prinfonum; i < prinfonum + buyu; i++) {
        //            for (int j = 0; j < 5; j++) {
        //                QTableWidgetItem *item= new QTableWidgetItem("");
        //                item->setBackground(QColor(180, 214, 242));
        //                item->setFont(font);
        //                item->setForeground(QColor(0, 0, 0));
        //                item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //                item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        //                ui->tableWidget_jzlog->setItem(i, j, item);
        //            }
        //        }

    }

}

void SettingDlg::initListview3()
{
    //设置列数和列宽
    //    int width = this->width();
    //    ui->tableWidget_dzlog->setColumnCount(5);
    //    ui->tableWidget_dzlog->setColumnWidth(0, 95);
    //    ui->tableWidget_dzlog->setColumnWidth(1, 277);
    //    ui->tableWidget_dzlog->setColumnWidth(2, 273);
    //    ui->tableWidget_dzlog->setColumnWidth(3, 93);
    //    ui->tableWidget_dzlog->setColumnWidth(4, 100);

    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);
    ui->tableWidget_dzlog->horizontalHeader()->setFont(font);

    ui->tableWidget_dzlog->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    ui->tableWidget_dzlog->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
    ui->tableWidget_dzlog->setSelectionMode(QAbstractItemView::NoSelection); //不选
    ui->tableWidget_dzlog->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tableWidget_dzlog->setAlternatingRowColors(true);
    ui->tableWidget_dzlog->verticalHeader()->setVisible(false);
    ui->tableWidget_dzlog->horizontalHeader()->setHighlightSections(false);

    //设置表头
    ui->tableWidget_dzlog->horizontalHeader()->setStyleSheet("QHeaderView::section{height:50px; background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE);color: #ABD6FF;padding-left: 4px;border: 1px solid #383838;}");
    ui->tableWidget_dzlog->verticalHeader()->setDefaultSectionSize(58);
    ui->tableWidget_dzlog->verticalScrollBar()->setStyleSheet("QScrollBar{background-color:rgb(1, 137, 255); width:20px;}"
                                                              "QScrollBar::handle{background-color:rgb(1, 137, 255); border:2px solid transparent; border-radius:5px;}"
                                                              "QScrollBar::handle:hover{background-color:rgb(1, 137, 255);}"
                                                              "QScrollBar::sub-line{background:transparent;}"
                                                              "QScrollBar::add-line{background:transparent;}");

    ui->tableWidget_dzlog->horizontalScrollBar()->setStyleSheet("QScrollBar{background-color:rgb(1, 137, 255); height:25px;}"
                                                                "QScrollBar::handle{background-color:rgb(1, 137, 255); border:2px solid transparent; border-radius:5px;}"
                                                                "QScrollBar::handle:hover{background-color:rgb(1, 137, 255);}"
                                                                "QScrollBar::sub-line{background:transparent;}"
                                                                "QScrollBar::add-line{background:transparent;}");

}

void SettingDlg::getData3()
{
    ui->tableWidget_dzlog->clearContents();
//    QStringList headText;
//    headText << tr("序号") << tr("时刻") << tr("订单") << tr("打印") << tr("X光机") << tr("合格") << tr("初判")
//             << tr("检出") << tr("总数") << tr("误报") << tr("复有针") << tr("复无针") << tr("终有针");
//    ui->tableWidget_dzlog->setColumnCount(13);
//    ui->tableWidget_dzlog->setHorizontalHeaderLabels(headText);

    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);

    QString riqi = ui->dateEdit_dzlog->text();

    if (db.isOpen()) {
        QString sql = QString("select * from duanzhen where riqi='%1' and sn ='%2' order by shijian")
                .arg(riqi, Start::getInstance().strSn);
        QSqlQuery query(db);
        query.exec(sql);

        int prinfonum = 0;
        if (query.last()) {
            prinfonum = query.at() + 1;
        }

        query.first();//重新定位指针到结果集首位
        query.previous();//如果执行query.next来获取结果集中数据，要将指针移动到首位的上一位。
        ui->tableWidget_dzlog->setRowCount(prinfonum);

        int i = 0;
        int youzhen = 0;
        int wuzhen = 0;
        int jieguo_num = 0;
        int xray_num = 0;
        int dy_num = 0;
        QList<QTableWidgetItem *> itemList;
        if (i == 0) {
            itemList << new QTableWidgetItem(tr("总数"));
            itemList << new QTableWidgetItem("-->");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem(QString::number(dy_num));
            itemList << new QTableWidgetItem(QString::number(xray_num));
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem(QString::number(youzhen));
            itemList << new QTableWidgetItem(QString::number(wuzhen));
            itemList << new QTableWidgetItem(QString::number(jieguo_num));

            for (int j = 0; j < itemList.size(); j++) {
                itemList[j]->setBackground(QColor(180, 214, 242));
                itemList[j]->setFont(font);
                itemList[j]->setForeground(QColor(0, 0, 0));
                itemList[j]->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                itemList[j]->setFlags(itemList[j]->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

                ui->tableWidget_dzlog->setItem(i, j, itemList[j]);
            }
            i++;
        }

        while(query.next()) {
            itemList.clear();
            itemList << new QTableWidgetItem(QString::number(i));

            QString shijian = query.value("shijian").toString();
            itemList << new QTableWidgetItem(shijian);

            QString po = query.value("po").toString();
            itemList << new QTableWidgetItem(po);

            QString dy = query.value("dy").toString();
            itemList << new QTableWidgetItem(dy);

            QString xray = query.value("xray").toString();
            itemList << new QTableWidgetItem(xray);

            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");
            itemList << new QTableWidgetItem("");

            int fuyouzhen = query.value("fuyouzhen").toInt();
            itemList << new QTableWidgetItem(QString::number(fuyouzhen));
            youzhen += fuyouzhen;

            int fuwuzhen = query.value("fuwuzhen").toInt();
            itemList << new QTableWidgetItem(QString::number(fuwuzhen));
            wuzhen += fuwuzhen;

            int jieguo = query.value("jieguo").toInt();
            itemList << new QTableWidgetItem(QString::number(jieguo));
            jieguo_num += jieguo;

            if (xray == "NG")
                xray_num++;
            if (dy == "N")
                dy_num++;

            QColor bkColor = QColor(128, 128, 128);
            if (jieguo == 1 && xray == "NG")
            {
                bkColor = QColor(255, 0, 0);
            } else if(jieguo == 1 && xray != "NG") {
                bkColor = QColor(255, 255, 0);
            }

            for (int j = 0; j < itemList.size(); j++) {
                itemList[j]->setBackground(bkColor);
                itemList[j]->setFont(font);
                itemList[j]->setForeground(QColor(0, 0, 0));
                itemList[j]->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                itemList[j]->setFlags(itemList[j]->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

                ui->tableWidget_dzlog->setItem(i, j, itemList[j]);
            }

            i++;
        }

        int rowSize = ui->tableWidget_dzlog->rowCount();
        if (rowSize > 0) {
            ui->tableWidget_dzlog->item(0, 4)->setText(QString::number(dy_num));
            ui->tableWidget_dzlog->item(0, 5)->setText(QString::number(xray_num));
            ui->tableWidget_dzlog->item(0, 10)->setText(QString::number(youzhen));
            ui->tableWidget_dzlog->item(0, 11)->setText(QString::number(wuzhen));
            ui->tableWidget_dzlog->item(0, 12)->setText(QString::number(jieguo_num));
        }

        //补齐剩余的行
        //        int buyu = (12 - prinfonum % 12);
        //        ui->tableWidget_dzlog->setRowCount(prinfonum + buyu);
        //        for (int i = prinfonum; i < prinfonum + buyu; i++) {
        //            for (int j = 0; j < 5; j++) {
        //                QTableWidgetItem *item= new QTableWidgetItem("");
        //                item->setBackground(QColor(180, 214, 242));
        //                item->setFont(font);
        //                item->setForeground(QColor(0, 0, 0));
        //                item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //                item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        //                ui->tableWidget_dzlog->setItem(i, j, item);
        //            }
        //        }

    }

}

void SettingDlg::initListview4()
{
    //设置列数和列宽
    //    int width = this->width();
    //    ui->tableWidget_polog->setColumnCount(5);
    //    ui->tableWidget_polog->setColumnWidth(0, 95);
    //    ui->tableWidget_polog->setColumnWidth(1, 277);
    //    ui->tableWidget_polog->setColumnWidth(2, 273);
    //    ui->tableWidget_polog->setColumnWidth(3, 93);
    //    ui->tableWidget_polog->setColumnWidth(4, 100);

    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);
    ui->tableWidget_polog->horizontalHeader()->setFont(font);

    ui->tableWidget_polog->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止编辑
    ui->tableWidget_polog->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
    ui->tableWidget_polog->setSelectionMode(QAbstractItemView::NoSelection); //不选
    ui->tableWidget_polog->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tableWidget_polog->setAlternatingRowColors(true);
    ui->tableWidget_polog->verticalHeader()->setVisible(false);
    ui->tableWidget_polog->horizontalHeader()->setHighlightSections(false);

    //设置表头
    ui->tableWidget_polog->horizontalHeader()->setStyleSheet("QHeaderView::section{height:50px; background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #0973AB,stop:1 #098DCE);color: #ABD6FF;padding-left: 4px;border: 1px solid #383838;}");
    ui->tableWidget_polog->verticalHeader()->setDefaultSectionSize(58);
    ui->tableWidget_polog->verticalScrollBar()->setStyleSheet("QScrollBar{background-color:rgb(1, 137, 255); width:20px;}"
                                                              "QScrollBar::handle{background-color:rgb(1, 137, 255); border:2px solid transparent; border-radius:5px;}"
                                                              "QScrollBar::handle:hover{background-color:rgb(1, 137, 255);}"
                                                              "QScrollBar::sub-line{background:transparent;}"
                                                              "QScrollBar::add-line{background:transparent;}");

    ui->tableWidget_polog->horizontalScrollBar()->setStyleSheet("QScrollBar{background-color:rgb(1, 137, 255); height:25px;}"
                                                                "QScrollBar::handle{background-color:rgb(1, 137, 255); border:2px solid transparent; border-radius:5px;}"
                                                                "QScrollBar::handle:hover{background-color:rgb(1, 137, 255);}"
                                                                "QScrollBar::sub-line{background:transparent;}"
                                                                "QScrollBar::add-line{background:transparent;}");

}

void SettingDlg::getData4()
{
    ui->tableWidget_polog->clearContents();
//    QStringList headText;
//    //    序号	日期	订单	数量	型体	码数	初判	检出	合格	达成率
//    headText << tr("序号") << tr("日期") << tr("订单") << tr("数量") << tr("型体") << tr("码数") << tr("初判")
//             << tr("金属") << tr("合格") << tr("达成率");
//    ui->tableWidget_polog->setColumnCount(10);
//    ui->tableWidget_polog->setHorizontalHeaderLabels(headText);

    QFont font;
    font.setPointSize(12);
    font.setWeight(QFont::DemiBold);

    QString riqi = ui->dateEdit_polog->text();

    if (db.isOpen()) {
        QString sql = QString("select * from esm_md_orderinfo_list where riqi='%1' and sn ='%2' ")
                .arg(riqi, Start::getInstance().strSn);
        QSqlQuery query(db);
        query.exec(sql);

        int num = 0;
        if (query.last()) {
            num = query.at() + 1;
        }

        query.first();//重新定位指针到结果集首位
        query.previous();//如果执行query.next来获取结果集中数据，要将指针移动到首位的上一位。
        ui->tableWidget_polog->setRowCount(num);

        int i = 0;
        QList<QTableWidgetItem *> itemList;
        while(query.next()) {
            itemList.clear();
            itemList << new QTableWidgetItem(QString::number(i+1));

            QString riqi = query.value("riqi").toString();
            itemList << new QTableWidgetItem(riqi);

            QString po = query.value("po").toString();
            itemList << new QTableWidgetItem(po);

            QString se_qty = query.value("se_qty").toString();
            itemList << new QTableWidgetItem(se_qty);

            QString style = query.value("style").toString();
            itemList << new QTableWidgetItem(style);

            QString size = query.value("size").toString();
            itemList << new QTableWidgetItem(size);

            QString chupan = query.value("chupan").toString();
            itemList << new QTableWidgetItem(chupan);

            QString jinshu = query.value("jinshu").toString();
            itemList << new QTableWidgetItem(jinshu);

            QString hege = query.value("hege").toString();
            itemList << new QTableWidgetItem(hege);

            float hgl = 0;
            hgl = hege.toFloat() / se_qty.toInt();
            itemList << new QTableWidgetItem(QString::number(hgl));

            QColor bkColor = QColor(180, 214, 242);

            for (int j = 0; j < itemList.size(); j++) {
                itemList[j]->setBackground(bkColor);
                itemList[j]->setFont(font);
                itemList[j]->setForeground(QColor(0, 0, 0));
                itemList[j]->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                itemList[j]->setFlags(itemList[j]->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);

                ui->tableWidget_polog->setItem(i, j, itemList[j]);
            }

            i++;
        }

        //补齐剩余的行
        //        int buyu = (12 - prinfonum % 12);
        //        ui->tableWidget_polog->setRowCount(prinfonum + buyu);
        //        for (int i = prinfonum; i < prinfonum + buyu; i++) {
        //            for (int j = 0; j < 5; j++) {
        //                QTableWidgetItem *item= new QTableWidgetItem("");
        //                item->setBackground(QColor(180, 214, 242));
        //                item->setFont(font);
        //                item->setForeground(QColor(0, 0, 0));
        //                item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        //                item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
        //                ui->tableWidget_polog->setItem(i, j, item);
        //            }
        //        }

    }

}

void SettingDlg::on_btExit_clicked()
{
    done(-1);
}

void SettingDlg::on_btSave_clicked()
{
    if (needreset) {
        done(1);
    } else {
        done(0);
    }

}


void SettingDlg::on_btAddjztime_clicked()
{
    QString starttime = ui->timeEdit_from->text();
    QString endtime = ui->timeEdit_to->text();
    QSqlQuery query(db);
    query.exec(QString("insert into jiaoyanshezhi(starttime,endtime, sn) values ('%1','%2','%3')").arg(starttime, endtime, Start::getInstance().strSn));
    getData1();
}


void SettingDlg::on_btAddjztimeupdate_clicked()
{
    int index = ui->tableWidget_jytime->currentRow();
    if (index >=0 && index < idList1.count() && db.isOpen()) {
        QString starttime = ui->timeEdit_from->text();
        QString endtime = ui->timeEdit_to->text();
        QSqlQuery query(db);
        QString sql = QString("UPDATE jiaoyanshezhi SET starttime='%1',endtime='%2' WHERE id=%3 ").arg(starttime, endtime, QString::number(idList1.at(index)));
        query.exec(sql);
        getData1();
    }

}

void SettingDlg::on_btAddjztimedelete_clicked()
{
    int index = ui->tableWidget_jytime->currentRow();
    if (index >=0 && index < idList1.count() && db.isOpen()) {
        QSqlQuery query(db);
        query.exec(QString("DELETE FROM jiaoyanshezhi WHERE id=%1").arg(QString::number(idList1.at(index))));
        getData1();
    }
}


void SettingDlg::on_btExcel_jzlog_clicked()
{
    QString riqi = ui->dateEdit_jzlog->text();
    QString fileName = QString("%1/workbook/check_%2.csv").arg(qApp->applicationDirPath(), riqi);

    QString dirName = qApp->applicationDirPath() + "/workbook";
    QDir dir(dirName);
    if(!dir.exists()) {
        dir.mkdir(dirName);
    }

    QFile file(fileName);
    file.open(QFile::WriteOnly | QIODevice::Text);
    QTextStream out(&file);

    out << tr("序号") + "," << tr("时刻")+ "," << tr("漏检") + ","<< tr("检出")+ "," << tr("结果") << QString("\n");
    int rowSize = ui->tableWidget_jzlog->rowCount();
    int columnSize = ui->tableWidget_jzlog->columnCount();
    for (int i = 0; i < rowSize; i++) {
        for (int j = 0; j < columnSize; j++) {
            QString msg = ui->tableWidget_jzlog->item(i, j)->text();
            out << msg;
            if (j == columnSize - 1) {
                out << QString("\n");
            } else {
                out << ",";
            }

        }
    }
    file.close();

    QMessageBox::information(nullptr, tr("导出校正记录文件"), fileName);

}


void SettingDlg::on_btExcel_dzlog_clicked()
{
    QString riqi = ui->dateEdit_dzlog->text();
    QString fileName = QString("%1/workbook/duanzhen_%2.csv").arg(qApp->applicationDirPath(), riqi);
    QString dirName = qApp->applicationDirPath() + "/workbook";
    QDir dir(dirName);
    if(!dir.exists()) {
        dir.mkdir(dirName);
    }

    QFile file(fileName);
    file.open(QFile::WriteOnly | QIODevice::Text);
    QTextStream out(&file);

    out << tr("序号") + "," << tr("时刻")+ "," << tr("订单") + ","<< tr("打印")+ "," << tr("X光机") + ","
        << tr("合格") + ","  << tr("初判") + ","  << tr("检出") + ","  << tr("总数") + ","  << tr("误报") + ","
        << tr("复有针") + ","  << tr("复无针") + ","  << tr("终有针")  << QString("\n");

    int rowSize = ui->tableWidget_dzlog->rowCount();
    int columnSize = ui->tableWidget_dzlog->columnCount();
    for (int i = 0; i < rowSize; i++) {
        for (int j = 0; j < columnSize; j++) {
            QString msg = ui->tableWidget_dzlog->item(i, j)->text();
            out << msg;
            if (j == columnSize - 1) {
                out << QString("\n");
            } else {
                out << ",";
            }

        }
    }
    file.close();

    QMessageBox::information(nullptr, tr("导出断针记录文件"), fileName);
}


void SettingDlg::on_btExcel_polog_clicked()
{
    QString riqi = ui->dateEdit_polog->text();
    QString fileName = QString("%1/workbook/po_%2.csv").arg(qApp->applicationDirPath(), riqi);
    QString dirName = qApp->applicationDirPath() + "/workbook";
    QDir dir(dirName);
    if(!dir.exists()) {
        dir.mkdir(dirName);
    }

    QFile file(fileName);
    file.open(QFile::WriteOnly | QIODevice::Text);
    QTextStream out(&file);

    out << tr("序号") + "," << tr("日期")+ "," << tr("订单") + ","<< tr("数量")+ ","
        << tr("型体") + "," << tr("码数")+ "," << tr("初判") + ","<< tr("金属")+ "," << tr("合格")+ ","
        << tr("达成率") << QString("\n");

    int rowSize = ui->tableWidget_polog->rowCount();
    int columnSize = ui->tableWidget_polog->columnCount();
    for (int i = 0; i < rowSize; i++) {
        for (int j = 0; j < columnSize; j++) {
            QString msg = ui->tableWidget_polog->item(i, j)->text();
            out << msg;
            if (j == columnSize - 1) {
                out << QString("\n");
            } else {
                out << ",";
            }

        }
    }
    file.close();

    QMessageBox::information(nullptr, tr("导出订单记录文件"), fileName);
}

void SettingDlg::on_btOK_jzlog_clicked()
{
    getData2();
}


void SettingDlg::on_btOK_dzlog_clicked()
{
    getData3();
}


void SettingDlg::on_btExcel_polog_ok_clicked()
{
    getData4();
}


void SettingDlg::on_comboBox_currentIndexChanged(int index)
{
    if (lastLanguage == index) {
        return;
    }
    lastLanguage = index;
    QSettings set(SettingDlg::ConfigFile, QSettings::IniFormat);
    QTranslator translator;
    QString baseName = "MD200_cn";

    switch (index) {
    default:
    case 0:
        set.setValue("language", "0");
        break;
    case 1:
        set.setValue("language", "1");
        baseName = "MD200_en";
        break;
    case 2:
        set.setValue("language", "2");
        baseName = "MD200_vi";
        break;
    }

    if (translator.load(":/i18n/" + baseName)) {
        if (qApp->installTranslator( &translator ) ) {
            ui->retranslateUi(this);
            needreset = true;
        }
    }

}


void SettingDlg::on_comboBox_2_currentIndexChanged(int index)
{
    QSettings set(SettingDlg::ConfigFile, QSettings::IniFormat);
    if (index == 0) {
        set.setValue("bHasRFID", 1);
    } else {
        set.setValue("bHasRFID", 0);
    }
}

